package com.ruoyi.experiment.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.experiment.domain.ExperimentDesign;
import com.ruoyi.experiment.domain.ExperimentRecord;
import com.ruoyi.experiment.service.IExperimentDesignService;
import com.ruoyi.experiment.service.IExperimentRecordService;
import com.ruoyi.system.api.RemoteAnimalInfomationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.experiment.domain.ExperimentData;
import com.ruoyi.experiment.service.IExperimentDataService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 实验数据Controller
 * 
 * @author gw
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/data")
public class ExperimentDataController extends BaseController
{
    @Autowired
    private IExperimentDataService experimentDataService;

    @Autowired
    private IExperimentRecordService experimentRecordService;

    @Autowired
    private IExperimentDesignService experimentDesignService;

    @Autowired
    private RemoteAnimalInfomationService remoteAnimalInfomationService;

    /**
     * 查询实验数据列表
     */
    @RequiresPermissions("Experiment:data:list")
    @GetMapping("/list")
    public TableDataInfo list(ExperimentData experimentData)
    {
        startPage();
        List<ExperimentData> list = experimentDataService.selectExperimentDataList(experimentData);
        for(ExperimentData data:list){
            ExperimentRecord experimentRecord = experimentRecordService.selectExperimentRecordByRecordId(data.getRecordId());
            ExperimentDesign experimentDesign = experimentDesignService.selectExperimentDesignByDesignId(experimentRecord.getDesignId());
            AjaxResult ajaxResult = remoteAnimalInfomationService.getInfo(data.getAnimalId());
            data.setStepNumber(experimentRecord.getStepNumber());
            data.setDesignName(experimentDesign.getDesignName());
            Map map = (Map)ajaxResult.get("data");
            data.setAnimalNumber(map.get("animalNumber").toString());
        }
        return getDataTable(list);
    }

    /**
     * 导出实验数据列表
     */
    @RequiresPermissions("Experiment:data:export")
    @Log(title = "实验数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExperimentData experimentData)
    {
        List<ExperimentData> list = experimentDataService.selectExperimentDataList(experimentData);
        ExcelUtil<ExperimentData> util = new ExcelUtil<ExperimentData>(ExperimentData.class);
        util.exportExcel(response, list, "实验数据数据");
    }

    /**
     * 获取实验数据详细信息
     */
    @RequiresPermissions("Experiment:data:query")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") Long dataId)
    {
        return success(experimentDataService.selectExperimentDataByDataId(dataId));
    }

    /**
     * 新增实验数据
     */
    @RequiresPermissions("Experiment:data:add")
    @Log(title = "实验数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExperimentData experimentData)
    {
        ExperimentRecord experimentRecord = experimentRecordService.selectExperimentRecordByRecordId(experimentData.getRecordId());
        experimentData.setAnimalId(experimentRecord.getAnimalId());
        experimentData.setMeasurementDate(experimentRecord.getRecordTime());
        experimentData.setParameter(experimentRecord.getParameter());
        experimentData.setValue(experimentRecord.getValue());
        return toAjax(experimentDataService.insertExperimentData(experimentData));
    }

    /**
     * 修改实验数据
     */
    @RequiresPermissions("Experiment:data:edit")
    @Log(title = "实验数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExperimentData experimentData)
    {
        ExperimentRecord experimentRecord = experimentRecordService.selectExperimentRecordByRecordId(experimentData.getRecordId());
        experimentData.setAnimalId(experimentRecord.getAnimalId());
        experimentData.setMeasurementDate(experimentRecord.getRecordTime());
        experimentData.setParameter(experimentRecord.getParameter());
        experimentData.setValue(experimentRecord.getValue());
        return toAjax(experimentDataService.updateExperimentData(experimentData));
    }

    /**
     * 删除实验数据
     */
    @RequiresPermissions("Experiment:data:remove")
    @Log(title = "实验数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable Long[] dataIds)
    {
        return toAjax(experimentDataService.deleteExperimentDataByDataIds(dataIds));
    }
}
